package demo.security;

import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.AnnotatedTypeMetadata;

import demo.security.JdbcTokenConfiguration.DemoJDBCTokenCondition;


@Configuration
@Conditional(DemoJDBCTokenCondition.class)
public class JdbcTokenConfiguration {
//	@Bean
//	public TokenStore tokenStore() {
//		return new JdbcTokenStore(this.dataSource);
//	}
	
//	@Bean
//    public UserDetailsService userDetailsService() {    	
//	//TODO: return new JdbcUserDetailsManager(this.dataSource);  	
//    }
	
	
	protected static class DemoJDBCTokenCondition extends SpringBootCondition{

		@Override
		public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
			
			ConditionMessage.Builder message = ConditionMessage.forCondition("tokenStore Condition");
			
			if("JDBC".equals(context.getEnvironment().getProperty("demo.tokenStore"))){
				return ConditionOutcome.match(message.foundExactly("JDBC tokenStore"));
			}
			
			return ConditionOutcome.noMatch(message.didNotFind("JDBC tokenStore").atAll());
		}
		
	}
}

